﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ApplicationSupport
{
    public enum ApplicationInstallType
    {
        USER,
        SYSTEM
    }

    public delegate void DirectoryUpdatedDelegate(object sender, EventArgs ev);

    public sealed class PathManager
    {
        private static volatile PathManager instance;
        private static object syncRoot = new object();

        private string m_applicationFolderPath;
        private string m_dataFolderPath;
        private string m_appName;

        public event DirectoryUpdatedDelegate DataFolderUpdated;
        public event DirectoryUpdatedDelegate ApplicationFolderUpdated;

        public string ApplicationFolder
        {
            get { return m_applicationFolderPath; }
            set 
            { 
                m_applicationFolderPath = value;
                if (ApplicationFolderUpdated != null)
                {
                    ApplicationFolderUpdated(this, new EventArgs());
                }
            }
        }

        public string DataFolder
        {
            get { return m_dataFolderPath; }
            set
            {
                m_dataFolderPath = value;
                if (DataFolderUpdated != null)
                {
                    DataFolderUpdated(this, new EventArgs());
                }
            }
        }

        private PathManager()
        {
            m_appName = Application.ProductName;
        }

        public static PathManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new PathManager();
                    }
                }

                return instance;
            }
        }

        public bool UserFolderExists()
        {
            return true;
        }

        public bool SystemFolderExists()
        {
            return true;
        }

        public bool FolderExists()
        {
            return true;
        }

        public bool FolderExists(string folder)
        {
            return true;
        }

        public static string CreateTemporaryFolder()
        {
            string newTemporaryFolder;

            do
            {
                newTemporaryFolder = Path.Combine(Path.GetTempPath() + Path.GetRandomFileName());
            } while (Directory.Exists(newTemporaryFolder));

            Directory.CreateDirectory(newTemporaryFolder);

            return newTemporaryFolder;
        }

        public static bool DeleteTemporaryFolder(string folderName)
        {
            bool directoryDeleted = false;

            try
            {
                // recursive delete of the supplied folder
                Directory.Delete(PathManager.Instance.DataFolder, true);
            }
            catch
            {
                // For now there is no error handling being implemented
            }

            return directoryDeleted;
        }

        public void CreateFolder()
        {
        }

        public void CreateFolder(string folder)
        {
        }

        public void SetType(ApplicationInstallType ait)
        {
            if (ait == ApplicationInstallType.SYSTEM)
            {
                m_applicationFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\" + m_appName;
            }
            else
            {
                m_applicationFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\" + m_appName;
            }
        }

        public string GetApplicationFile(string fileName)
        {
            return m_applicationFolderPath + @"\" + fileName;
        }

        public string GetDataFile(string fileName)
        {
            return m_dataFolderPath + @"\" + fileName;
        }

        public string MyDocuments
        {
            get
            {
                return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            }
        }
    }
}
